#include "xtcpform.h"
#include "ui_xtcpform.h"
#include "mainform.h"
#include "tableview.h"
#include "itemdelegate.h"
#include <QStandardItemModel>
#include <QSettings>

xtcpForm::xtcpForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::xtcpForm)
{
    ui->setupUi(this);

    xtcpTable = new TableView(380,280,xtcp_length);
    tableInit(); //表格初始化
    ui->stackedWidget->addWidget(xtcpTable);
    settings = new QSettings("frp","xtcp");
    if(settings->value("xtcpSetup").toInt() == 1)
        recData();
}

xtcpForm::~xtcpForm(){
    delete ui;
    saveData();
    settings->setValue("xtcpSetup",1);
}

void xtcpForm::tableInit(){
    QStringList items;
    items.append("true");
    items.append("false");

    QStringList items2;
    items2.append("server");
    items2.append("client");

    xtcpTable->model->setHeaderData(xtcp_sw  ,Qt::Horizontal,       QObject::tr("启用"));
    xtcpTable->model->setHeaderData(xtcp_ip  ,Qt::Horizontal,       QObject::tr("ip"));
    xtcpTable->model->setHeaderData(xtcp_name,Qt::Horizontal,       QObject::tr("配对码"));
    xtcpTable->model->setHeaderData(xtcp_sORc,Qt::Horizontal,       QObject::tr("服务"));
    xtcpTable->model->setHeaderData(xtcp_port,Qt::Horizontal,       QObject::tr("端口"));
//    xtcpTable->model->setHeaderData(xtcp_sk  ,Qt::Horizontal,       QObject::tr("SK秘钥"));
    xtcpTable->model->setHeaderData(xtcp_encryption,Qt::Horizontal, QObject::tr("加密"));
    xtcpTable->model->setHeaderData(xtcp_compression,Qt::Horizontal,QObject::tr("压缩"));

    for(int col = 0; col < xtcp_length; col++){
        QModelIndex index = xtcpTable->model->index(0, col, QModelIndex());
        if(col == xtcp_sw){
            ComboDelegate *combo3 = new ComboDelegate;
            combo3->setItems(items);
            xtcpTable->setItemDelegateForColumn(col,combo3);
            xtcpTable->model->setData(index,"true");
        }
        if(col == xtcp_ip){
            xtcpTable->model->setData(index,"127.0.0.1");
        }
        if(col == xtcp_name){
            xtcpTable->model->setData(index,"p2p");
        }

        if(col == xtcp_sORc){
            ComboDelegate *combo4 = new ComboDelegate;
            combo4->setItems(items2);
            xtcpTable->setItemDelegateForColumn(col,combo4);
            xtcpTable->model->setData(index,"server");
        }

        if( col == xtcp_port){
            xtcpTable->model->setData(index,"5550");
        }

//        if(col == xtcp_sk){
//            xtcpTable->model->setData(index,"123456");
//        }

        if(col == xtcp_encryption){
            ComboDelegate *combo1 = new ComboDelegate;
            combo1->setItems(items);
            xtcpTable->setItemDelegateForColumn(col,combo1);
            xtcpTable->model->setData(index,"false");
        }
        if(col == xtcp_compression){
            ComboDelegate *combo2 = new ComboDelegate;
            combo2->setItems(items);
            xtcpTable->setItemDelegateForColumn(col,combo2);
            xtcpTable->model->setData(index,"false");
        }
    }
    /*列宽*/
    qint16 tableWidth =  ui->stackedWidget->geometry().width();
    qint16 Width = tableWidth / (xtcp_length + 1) - 2;
    xtcpTable->setColumnWidth(xtcp_sw  ,        Width)  ;
    xtcpTable->setColumnWidth(xtcp_name,        Width)  ;
    xtcpTable->setColumnWidth(xtcp_ip,          Width*2);
    xtcpTable->setColumnWidth(xtcp_sORc,        Width)  ;
    xtcpTable->setColumnWidth(xtcp_port,        Width)  ;
    xtcpTable->setColumnWidth(xtcp_encryption,  Width ) ;
    xtcpTable->setColumnWidth(xtcp_compression, Width ) ;
    //xtcpTable->setColumnWidth(xtcp_sk  ,      Width)  ;
}

void xtcpForm::on_addPushButton_clicked(){
    QModelIndex index;
    xtcpTable->model->insertRow(xtcpTable->currentIndex().row()+1); //加一行

    index = xtcpTable->model->index(xtcpTable->currentIndex().row()+1,xtcp_sw, QModelIndex()); //获取index
    xtcpTable->model->setData(index,"true");//设置初始值

    index = xtcpTable->model->index(xtcpTable->currentIndex().row()+1,xtcp_encryption, QModelIndex());
    xtcpTable->model->setData(index,"false");//设置初始值

    index = xtcpTable->model->index(xtcpTable->currentIndex().row()+1,xtcp_compression, QModelIndex());
    xtcpTable->model->setData(index,"false");//设置初始值

    index = xtcpTable->model->index(xtcpTable->currentIndex().row()+1,xtcp_sORc, QModelIndex());
    xtcpTable->model->setData(index,"server");//设置初始值
}


void xtcpForm::on_delePushButton_clicked(){
    xtcpTable->model->removeRow(xtcpTable->currentIndex().row());
}

void xtcpForm::saveData(){
    settings->setValue("row",xtcpTable->model->rowCount());
    for( int i = 0; i < xtcpTable->model->rowCount() ; i++){
        for( int j = 0;j < xtcp_length; j++ ){
            settings->setValue("row"+QString::number(i)+QString::number(j),xtcpTable-> GetItemData(i,j));
        }
    }
}
void xtcpForm::recData(){
    int oldRow = (settings->value("row")).toInt();
    int beginRow = xtcpTable->model->rowCount();
    if(oldRow > beginRow){
        for( int i = 0 ; i < (oldRow - beginRow) ; i++){
            xtcpTable->model->insertRow(beginRow);
        }
    }
    else {
        for( int i = 0 ; i < (beginRow - oldRow) ; i++){
            xtcpTable->model->insertRow(beginRow);
        }
    }
    for( int i = 0; i < xtcpTable->model->rowCount() ; i++){
        for( int j = 0;j < xtcp_length; j++ ){
            QModelIndex index = xtcpTable->model->index(i, j, QModelIndex());
            xtcpTable->model->setData(index, (settings->value("row"+QString::number(i)+QString::number(j))).toString());
        }
    }
}


void xtcpForm::on_startButton_clicked(){
    mainw->on_startButton_clicked();
}

void xtcpForm::on_ResetButton_clicked(){
    mainw->on_ResetButton_clicked();
}

